﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BankApplication.BusinessLogic;
using BusinessLogic.BusinessEntity.Entities;

namespace BankApplication.UI
{
	public partial class CurrencyConversionForm : Form
	{
		public CurrencyConversionForm()
		{
			InitializeComponent();
		}

		public void ValidateForm()
		{
			decimal conversionRate;

			if (SourceCurrencyComboBox.SelectedIndex == -1)
			{
				throw new ValidationException("The source currency is not selected!");
			}

			if (DestinationCurrencyCombobox.SelectedIndex == -1)
			{
				throw new ValidationException("The destination currency isn't selected!");
			}

			if (SourceCurrencyComboBox.Text == DestinationCurrencyCombobox.Text)
			{
				throw new ValidationException("The source and the target currencies cannot be the same!");
			}

			try
			{
				conversionRate = Convert.ToDecimal(ConversionRateTextBox.Text);
			}
			catch (Exception)
			{
				throw new ValidationException(String.Format("Incorrect conversion rate value: {0}!" +
					" It must be a valid decimal value! (e.g. 10,34)", ConversionRateTextBox.Text));
			}

			if (conversionRate <= 0)
			{
				throw new ValidationException("Conversion rate must be a positive decimal value! (e.g. 10,34)");
			}
		}

		private void CloseButton_Click(object sender, EventArgs e)
		{
			Close();
		}

		private void AddCurrencyConversionButton_Click(object sender, EventArgs e)
		{
			try
			{
				ValidateForm();

				var conversion = new CurrencyConversion();
				conversion.SourceCurrency = currencies[SourceCurrencyComboBox.SelectedIndex];
				conversion.TargetCurrency = currencies[DestinationCurrencyCombobox.SelectedIndex];
				conversion.ConversionRate = Convert.ToDecimal(ConversionRateTextBox.Text);

				new ServerInteractionExceptionCatcher().Execute(() =>
				{
					CurrenciesManager.CreateCurrencyConversion(conversion, SessionManager.Instance.SessionTicket);

					Close();
				});
			}
			catch (ValidationException exception)
			{
				MessageBox.Show(exception.Message, "Form validation failed!", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		
		private void SourceCurrencyComboBox_DropDown(object sender, EventArgs e)
		{

			new ServerInteractionExceptionCatcher().Execute(() =>
			{
				currencies = CurrenciesManager.GetCurrencies(SessionManager.Instance.SessionTicket);
				var currencyNames = currencies.Select(currency => currency.Name).ToList();
				SourceCurrencyComboBox.DataSource = currencyNames;
			});
		}

		private void DestinationCurrencyCombobox_DropDown(object sender, EventArgs e)
		{
			new ServerInteractionExceptionCatcher().Execute(() =>
			{
				currencies = CurrenciesManager.GetCurrencies(SessionManager.Instance.SessionTicket);
				var currencyNames = currencies.Select(currency => currency.Name).ToList();
				DestinationCurrencyCombobox.DataSource = currencyNames;
			});

		}

		private IList<Currency> currencies;
	}
}
